package com.hefu.virtuallife.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.dz.utlis.AndroidUtils;
import com.hefu.virtuallife.R;

import java.util.List;

/**
 *creat_user: zhengzaihong
 *email:1096877329@qq.com
 *creat_date:  2018/8/13
 *creat_time: 10:39
 *describe: pop 简单封装下
 **/
public class PopWindHelper {

    private PopupWindow popWin;
    private PopclickListener listener;
    private View view;
    private Context mContext;

    public PopWindHelper(Context mContext){
        this.mContext =mContext;
    }

    public PopWindHelper initPopAt(int containid, int measure[]) {
        view = LayoutInflater.from(mContext).inflate(containid, null);
        popWin = new PopupWindow(view, measure[0], WindowManager.LayoutParams.WRAP_CONTENT);
        popWin.setFocusable(false);
       // popWin.setOutsideTouchable(true);
        popWin.setBackgroundDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.transparent)));
         setWindowAlpha(0.5f);
        popWin.setOnDismissListener(() -> {
            setWindowAlpha(1f); //Window设置为全透明
        });
        return this;
    }

    public PopWindHelper initPop(int containid) {
        view = LayoutInflater.from(mContext).inflate(containid, null);

        popWin = new PopupWindow(view, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popWin.setFocusable(true);
        setWindowAlpha(0.5f);
        popWin.setBackgroundDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.transparent)));
        // popWin!!.animationStyle = R.style.storeImageChooseStyle
        popWin.setOnDismissListener(() -> setWindowAlpha(1f));

        return this;
    }


    public PopWindHelper bindAdapter(ListView listView, List<Object> data, int itmeid) {
        listView.setAdapter(new TypeAdapter(mContext, data, itmeid));
        listView.setOnItemClickListener((adapterView, view, p, l) -> {
            if (null != listener) {
                listener.callback(p);
                closepop();
            }
        });
        return this;
    }


    public PopWindHelper bindClick(PopclickListener listener)

    {
        this.listener = listener;
        return this;
    }

    public View parentView()

    {
        return view;
    }


    public interface PopclickListener {
        void callback(int position);
    }

    /**
     * 关闭dialog
     */
    public void closepop() {
        if (null != popWin) {
            popWin.dismiss();
        }
    }

    /**
     * 指定显示位置
     */
    public PopWindHelper show(View rootview) {
        popWin.showAsDropDown(rootview);
        return this;
    }

    /**
     * 指定显示位置
     * 默认在底部显示
     */
    public PopWindHelper showAt(View rootview, int gravity, int x, int y) {
        popWin.showAtLocation(rootview, gravity, x, y);
        return this;
    }


    private void setWindowAlpha(Float alpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = alpha;
        (((Activity) mContext)).getWindow().setAttributes(lp);
    }

    private AdapterCallBackViewHolder adapterCallBackViewHolder;

    public PopWindHelper setAdapterCallBackViewHolder(AdapterCallBackViewHolder adapterCallBackViewHolder) {
        this.adapterCallBackViewHolder = adapterCallBackViewHolder;
        return this;
    }

    public interface AdapterCallBackViewHolder {
        void callbackholder(TypeAdapter.ViewHolder viewHolder, int position);

    }

    public class TypeAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<Object> type;
        private int itmeid;

        public TypeAdapter(Context mContext, List<Object> type, int itmeid) {
            this.inflater = LayoutInflater.from(mContext);
            this.type = type;
            this.itmeid = itmeid;
        }


        @Override
        public int getCount() {
            return null == type ? 0 : type.size();
        }

        @Override
        public Object getItem(int i) {
            return type.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (null == view) {
                view = inflater.inflate(itmeid, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            if (null != adapterCallBackViewHolder) {
                adapterCallBackViewHolder.callbackholder(holder, i);
            }
            return view;
        }

        public class ViewHolder {
            public View view;

            public ViewHolder(View view) {
                this.view = view;
            }
        }
    }
}
